
clc
global xt
global yt
global y1
global entrenamiento_yt_desnorm
global iteracion
iteracion=0;
estacion='soleado'

input=[];
day=[];
prueba_normalizada=[];
input=readmatrix('2021-2022-2023.xlsx','Sheet','diciembre','Range','P5:AE210244');
dimension_input=size(input)
longitud=dimension_input(1)
j=1;
for i=1:longitud
    if input(i,dimension_input(2))>0
        input_sin_ceros(j,:)=input(i,:);
        j=j+1;
    end
end

entrada_temp=input_sin_ceros(:,1);
entrada_rad=input_sin_ceros(:,14);
salida=input_sin_ceros(:,16); 
x1=[entrada_temp entrada_rad];
y1=salida;

day=readmatrix('2021-2022-2023.xlsx','Sheet','2021-2022-2023','Range','P177989:AE178276');
prueba_temp=day(:,1);
prueba_rad=day(:,14);
prueba=[prueba_temp prueba_rad];
medida=day(:,16);


y2=(y1-min(y1))./(max(y1)-min(y1)); 
for i=1:2
    x2(:,i)=(x1(:,i)-min(x1(:,i)))/(max(x1(:,i))-min(x1(:,i)));
    prueba_normalizada(:,i)=(prueba(:,i)-min(x1(:,i)))/(max(x1(:,i))-min(x1(:,i)));
end
xt=x2';
yt=y2';

Num_max_neuronas=100;
nvar=(1+2+1+(Num_max_neuronas*4+1));
options=optimoptions('ga','PlotFcn','gaplotbestf','MutationFcn', {@mutationuniform, 0.3},'PopulationSize',nvar);
tic
[x,fval,exitflag,output,population,score] =ga(@A_2entradas,nvar,[],[],[],[],[],[],[],[],options);
tiempo=toc

if ceil(abs(x(1)))>100
    hiddenLayerSize=100;
else hiddenLayerSize=ceil(abs(x(1)));
end
hiddenLayerSize 
valor_transferFcn_1=ceil(abs(x(2)));
valor_transferFcn_2=ceil(abs(x(3)));
valor_trainFcn=ceil(abs(x(4)));
if valor_transferFcn_1>15 | valor_transferFcn_2>15 | valor_trainFcn>9
    valor_transferFcn_1=15;
    valor_transferFcn_2=15;
    valor_trainFcn=9;
end

x_h=x(5:length(x));
longitud_x_h=length(x_h)
x_v=x(5:length(x))';

net = feedforwardnet(hiddenLayerSize);

net.divideParam.trainRatio=70/100;
net.divideParam.valRatio=15/100;
net.divideParam.testRatio=15/100;

net = configure(net,xt,yt);
net.layers{1}.size=hiddenLayerSize;
vector=zeros(hiddenLayerSize,2);
p=1;
for i=1:hiddenLayerSize
    for j=1:2
        vector(i,j)=x_h(p);
        p=1+p;
    end
end
net.IW{1}=vector;
net.LW{2}=x_h(p:p+hiddenLayerSize-1);
net.b{1}=x_v(p+hiddenLayerSize:p+hiddenLayerSize*2-1);
net.b{2}=x_h(p+hiddenLayerSize*2);

switch valor_trainFcn
    case 1
        net.trainFcn='trainlm';
    case 2
        net.trainFcn='trainbfg';
    case 3
        net.trainFcn='trainrp';
    case 4
        net.trainFcn='trainscg';
    case 5
        net.trainFcn='traincgb';
    case 6
        net.trainFcn='traincgf';
    case 7
        net.trainFcn='traincgp';
    case 8
        net.trainFcn='trainoss';
    case 9
        net.trainFcn='traingdx';
end
switch valor_transferFcn_1
    case 1
        net.layers{1}.transferFcn='compet';
    case 2
        net.layers{1}.transferFcn='elliotsig';
    case 3
        net.layers{1}.transferFcn='hardlim';
    case 4
        net.layers{1}.transferFcn='hardlims';
    case 5
        net.layers{1}.transferFcn='logsig';
    case 6
        net.layers{1}.transferFcn='netinv';
    case 7
        net.layers{1}.transferFcn='poslin';
    case 8
        net.layers{1}.transferFcn='purelin';
    case 9
        net.layers{1}.transferFcn='radbas';
    case 10
        net.layers{1}.transferFcn='radbasn';
    case 11
        net.layers{1}.transferFcn='satlin';
    case 12
        net.layers{1}.transferFcn='satlins';
    case 13
        net.layers{1}.transferFcn='softmax';
    case 14
        net.layers{1}.transferFcn='tansig';
    case 15
        net.layers{1}.transferFcn='tribas';
end
switch valor_transferFcn_2
    case 1
        net.layers{2}.transferFcn='compet';
    case 2
        net.layers{2}.transferFcn='elliotsig';
    case 3
        net.layers{2}.transferFcn='hardlim';
    case 4
        net.layers{2}.transferFcn='hardlims';
    case 5
        net.layers{2}.transferFcn='logsig';
    case 6
        net.layers{2}.transferFcn='netinv';
    case 7
        net.layers{2}.transferFcn='poslin';
    case 8
        net.layers{2}.transferFcn='purelin';
    case 9
        net.layers{2}.transferFcn='radbas';
    case 10
        net.layers{2}.transferFcn='radbasn';
    case 11
        net.layers{2}.transferFcn='satlin';
    case 12
        net.layers{2}.transferFcn='satlins';
    case 13
        net.layers{2}.transferFcn='softmax';
    case 14
        net.layers{2}.transferFcn='tansig';
    case 15
        net.layers{2}.transferFcn='tribas';
end
net
entrenamiento_yt=(net(xt))';
entrenamiento_yt_desnorm=entrenamiento_yt*(max(y1)-min(y1))+min(y1);
RMSE_train=sqrt((sum((entrenamiento_yt_desnorm-y1).^2))/length(y1))

prueba_output_normalizada=net(prueba_normalizada');
p_pv_sin_corregir=prueba_output_normalizada*(max(y1)-min(y1))+min(y1);

for i=1:length(p_pv_sin_corregir)
    if p_pv_sin_corregir(i)<=0 ||medida(i)==0
        p_pv(i)=0;
    else
        p_pv(i)=p_pv_sin_corregir(i);
    end
end
RMSE=sqrt((sum((p_pv'-medida).^2))/length(medida))

prediccion=p_pv;
figure()
plotregression(medida,prediccion,'Regression')

figure()
plot(medida,'b')
hold on
plot(p_pv,'r')
hold off
legend('medida','predicción(p_pv)')

filename = 'Resultados_train_diciembre.xlsx';
nombre={'x optimizada (parámteros ANN):'};
writecell(nombre,filename,'Sheet',1,'Range','A1');
writematrix(x,filename,'Sheet',1,'Range','B1');
nombre={'Population:'};
writecell(nombre,filename,'Sheet',1,'Range','A2');
nombre={'Nº nuronas (redondeo al inmediato superior):','TransferFcn_1 (redondeo al inmediato superior):','TransferFcn_2 (redondeo al inmediato superior):','TrainFcn (no uso):'};
writecell(nombre,filename,'Sheet',1,'Range','A3');
writematrix(population,filename,'Sheet',1,'Range','A4');
nombre={'RMSE train optimizada:'};
writecell(nombre,filename,'Sheet',2,'Range','A1');
writematrix(fval,filename,'Sheet',2,'Range','B1');
nombre={'Score:'};
writecell(nombre,filename,'Sheet',2,'Range','A2');
writematrix(score,filename,'Sheet',2,'Range','A3');
nombre={'Medida:'};
writecell(nombre,filename,'Sheet',3,'Range','A1');
writematrix(medida,filename,'Sheet',3,'Range','A2');
nombre={'Predicción:'};
writecell(nombre,filename,'Sheet',3,'Range','B1');
writematrix(p_pv',filename,'Sheet',3,'Range','B2');